package com.td.erhome.ui.article;

import android.app.Dialog;
import android.graphics.Bitmap;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.td.erhome.R;
import com.td.erhome.base.BaseActivity;
import com.td.erhome.utils.DialogUtils;

import butterknife.Bind;
import butterknife.OnClick;

public class ArticleContentActivity extends BaseActivity {

    @Bind(R.id.img_back)
    ImageView mImgBack;
    @Bind(R.id.re_title)
    RelativeLayout nAltha;
    @Bind(R.id.tv_title)
    TextView mTvTitle;
    @Bind(R.id.web_view)
    WebView mWebView;
    private Dialog mMDialog;
    private String mPath;
    private String mTitle;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_article_content;
    }

    @Override
    protected void initView() {

        nAltha.setBackgroundColor(getResources().getColor(R.color.color_0b9676));
        nAltha.getBackground().setAlpha(255);
        mPath = getIntent().getStringExtra("url");
        mTitle = getIntent().getStringExtra("title");
        mTvTitle.setText(mTitle);

        mWebView.getSettings().setAllowFileAccess(true);  //应许读取文件
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setAppCacheEnabled(true);
        mWebView.getSettings().setDomStorageEnabled(true);
        mWebView.getSettings().setBlockNetworkImage(false);
        mWebView.getSettings().setUseWideViewPort(true);

        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                if (mMDialog == null) {
                    mMDialog = new DialogUtils().loading(ArticleContentActivity.this);
                }
                mMDialog.setCancelable(true);
                mMDialog.setCanceledOnTouchOutside(false);
                mMDialog.show();
            }
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                if (mMDialog != null) {
                    mMDialog.dismiss();
                }
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
                if (mMDialog != null) {
                    mMDialog.dismiss();
                }
            }
        });
        mWebView.loadUrl(mPath);
    }

    @Override
    protected void releaseResource() {

    }

    @OnClick(R.id.img_back)
    public void onViewClicked() {
        finish();
    }
}
